package com.xzx.mapreduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/7/13 20:49
 */
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable outputValue = new IntWritable();

    // 相同的key都归并了一个values集合中了
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException,
            InterruptedException {
        // 遍历values计算数量
        int count = 0;
        for (IntWritable value : values) {
            count++;
        }
        outputValue.set(count);

        //将结果输出
        context.write(key, outputValue);
    }
}
